from django.contrib import admin
import datetime
from app_srv_monitor.home.models import ApplicationGrouping
from app_srv_monitor.home.models import SubGroup
from app_srv_monitor.home.models import Application
from app_srv_monitor.home.models import ApplicationTestPage
from app_srv_monitor.home.models import ServerModel
from app_srv_monitor.home.models import ServerName
from app_srv_monitor.home.models import ApplicationToServer
from django.contrib.auth.models import User

def get_current_user(request):
    theUser=request.user
    return theUser

class ApplicationToServerMapping(admin.ModelAdmin):
    fieldsets = [('Basic Matching',{
                                    'fields':('AppURI','ServerName','Listener_Port')
                                    })]
    fk_name = "AppURI"
    list_display = ('AppURI','ServerName')
    def save_model(self, request, obj, form, change):
            obj.MAdded_User = str(request.user)
            obj.MLast_User = str(request.user)
            obj.save()
admin.site.register(ApplicationToServer,ApplicationToServerMapping)

class ApplicationAdmin(admin.ModelAdmin):
        fieldsets = [
                     ('Basic Information',{
                            'fields':('AGrouping','App_Name','App_URI','App_Listener_Port','App_Version_File','App_Description')
                            })
                     ]
        
        list_display = ('App_Name','AGrouping','AAdded_Date','ALast_Date')
        def save_model(self, request, obj, form, change):
            obj.save()

admin.site.register(Application,ApplicationAdmin)

class GroupAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Basic Info', {
            'fields': ('Grouping_Name', 'Grouping_Description')
        }),
    ]
    def save_model(self, request, obj, form, change):
        obj.AGAdded_User = str(request.user)
        obj.AGLast_User = str(request.user)
        obj.save()
#    list_display = ('Grouping_Name','AGAdded_Date','AGAdded_User','AGLast_Date','AGLast_User')
    list_display = ('Grouping_Name','AGAdded_Date','AGLast_Date')

admin.site.register(ApplicationGrouping, GroupAdmin)

class SubGroupAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Basic Info', {
            'fields': ('ParentGrouping', 'SubGroupName')
        }),
    ]
    def save_model(self, request, obj, form, change):
        obj.SGAdded_User = str(request.user)
        obj.SGLast_User = str(request.user)
        obj.save()
    list_display = ('ParentGrouping','SubGroupName','SGAdded_Date','SGAdded_User','SGLast_Date','SGLast_User')

admin.site.register(SubGroup, SubGroupAdmin)



class ServModel(admin.ModelAdmin):
    fieldsets = (
        ('Server Type Information',{
                'fields': ('SMName', 'SMVersion_Number')
        }), )
    list_display = ('SMName','SMVersion_Number','SMAdded_Date','SMLast_Date')
    def save_model(self, request, obj, form, change):
        obj.SMAdded_User = str(request.user)
        obj.SMLast_User = str(request.user)
        obj.save()

admin.site.register(ServerModel, ServModel)

class ServerNameAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Server Information',{
                'fields': ('FQDN','SVersion_Number','IP_Address', 'SGrouping','Server_Class')
                }), ]
    radio_fields = {'Server_Class': admin.VERTICAL}
    list_display = ('FQDN','SAdded_User','SAdded_Date','SLast_User','SLast_Date','SGrouping')
    def save_model(self, request, obj, form, change):
        obj.SAdded_User = str(request.user)
        obj.SLast_User = str(request.user)
        obj.save()

admin.site.register(ServerName, ServerNameAdmin)

class TestPage(admin.ModelAdmin):
    fieldsets = [
        ('Test Page Information',{
                'fields': ('App_Name','TPName','Test_Located','Test_Instruction',)
                }), ]
    list_display = ('TPName','Test_Located','TPLast_User','TPLast_Date')
    def save_model(self, request, obj, form, change):
        obj.TPAdded_User = str(request.user)
        obj.TPLast_User = str(request.user)
        obj.save()

admin.site.register(ApplicationTestPage, TestPage)
